package com.test.rpc;

import com.test.rpc.common.ReflectionUtils;
import com.test.rpc.protocol.Request;
import com.test.rpc.protocol.ServiceDescriptor;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class ServiceManager {
    private Map<ServiceDescriptor, ServiceInstance> services;

    public ServiceManager() {
        this.services = new ConcurrentHashMap<ServiceDescriptor, ServiceInstance>();
    }
    public <T> void register(Class<T> interfaceClass ,T bean){
        Method[] methods = ReflectionUtils.getPublicMethods(interfaceClass);
        for (Method method : methods) {
            ServiceInstance serverInstance = new ServiceInstance(bean, method);
            ServiceDescriptor sdp = ServiceDescriptor.from(interfaceClass, method);
            services.put(sdp,serverInstance);
            log.info("register service: {}:{}", sdp.getClazz(), sdp.getMethod());
        }
    }
    //查找服务
    public ServiceInstance lookup(Request request) {
        ServiceDescriptor sdp = request.getService();
        return services.get(sdp);
    }

}
